package testcode;

import com.example.start.Address;
import com.example.start.Contacts;
import com.example.start.PeopleInfo;
import com.google.protobuf.Any;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

public class TestWrite {
    public static void main(String[] args) throws IOException {
        Contacts.Builder contactsBuilder = Contacts.newBuilder();
        // 读取已存在的contacts
        try {
            contactsBuilder.mergeFrom(new
                    FileInputStream("src/main/java/com/example/start/contacts.bin"));
        } catch (FileNotFoundException e) {
            System.out.println("contacts.bin not found. Creating a new file.");
        }

        // 新增⼀个联系⼈
        contactsBuilder.addContacts(addPeopleInfo());
        // 将新的contacts写回磁盘
        FileOutputStream output = new FileOutputStream("src/main/java/com/example/start/contacts.bin");
        contactsBuilder.build().writeTo(output);
        output.close();
    }

    private static PeopleInfo addPeopleInfo() {
        Scanner scan = new Scanner(System.in);
        PeopleInfo.Builder peopleBuilder = PeopleInfo.newBuilder();

        System.out.println("-------------新增联系⼈-------------");
        System.out.print("请输⼊联系⼈姓名: ");
        String name = scan.nextLine();
        peopleBuilder.setName(name);

        System.out.print("请输⼊联系⼈年龄: ");
        int age = scan.nextInt();
        peopleBuilder.setAge(age);
        scan.nextLine();

        for (int i = 0; ; i++) {
            System.out.print("请输⼊联系⼈电话" + (i + 1) + "(只输⼊回⻋完成电话新 增): ");
            String number = scan.nextLine();
            if (number.isEmpty()) {
                break;
            }
            PeopleInfo.Phone.Builder phoneBuilder = PeopleInfo.Phone.newBuilder();
            phoneBuilder.setNumber(number);

            System.out.print("选择此电话类型 (1、移动电话 2、固定电话) : ");
            int type = scan.nextInt();
            scan.nextLine();
            switch (type) {
                case 1:
                    phoneBuilder.setType(PeopleInfo.Phone.PhoneType.MP);
                    break;
                case 2:
                    phoneBuilder.setType(PeopleInfo.Phone.PhoneType.TEL);
                    break;
                default:
                    System.out.println("⾮法选择，使⽤默认值！");
                    break;
            }
            peopleBuilder.addPhone(phoneBuilder);
        }
        Address.Builder addressBulider = Address.newBuilder();
        System.out.println("请输入联系人家庭地址：");
        String homeAddress = scan.nextLine();
        addressBulider.setHomeAddress(homeAddress);
        System.out.println("请输入联系人单位地址");
        String unitAddress = scan.nextLine();
        addressBulider.setUnitAddress(unitAddress);
        peopleBuilder.setData(Any.pack(addressBulider.build()));

        System.out.println("请选择要添加的其他联系方式（1.qq号 2.微信号）：");
        int otherContact = scan.nextInt();
        scan.nextLine();
        if (1 == otherContact) {
            System.out.println("请输入qq号：");
            String qq = scan.nextLine();
            peopleBuilder.setQq(qq);
        } else if (2 == otherContact) {
            System.out.println("请输入微信号");
            String wechat = scan.nextLine();
            peopleBuilder.setWechat(wechat);
        } else {
            System.out.println("无效选择，设置失败！");
        }

        for (int i = 0; ; i++) {
            System.out.println("请输入备注：" + (i + 1) + "标题（只输入回车完成备注新增）：");
            String key = scan.nextLine();
            if (key.isEmpty()) {
                break;
            }
            System.out.println("请输入备注内容：");
            String value = scan.nextLine();
            peopleBuilder.putRemark(key, value);
        }

        System.out.println("-------------添加联系⼈成功-------------");
        return peopleBuilder.build();
    }
}
